package com.erp.gather.controller;

import com.erp.gather.domain.ErpCollectProductCategory;
import com.erp.gather.domain.bo.ErpCollectProductCategoryBo;
import com.erp.gather.domain.dto.ErpCollectProductCategoryDto;
import com.erp.gather.domain.vo.ErpCollectProductCategoryVo;
import com.erp.gather.service.IErpCollectProductCategoryService;
import com.ruoyi.common.core.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.web.oldController.BaseController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.InnerAuth;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 采集商品类目
 *
 * @author erp
 * @date 2023-03-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/gather/collectProductCategory")
public class ErpCollectProductCategoryController extends BaseController {

    private final IErpCollectProductCategoryService iErpCollectProductCategoryService;


    /**
     * 查看类目列表
     */
    @GetMapping("/getCategoryList/{categoryList}")
    public R<Object> getCategoryList(@PathVariable("categoryList") String categoryList) {
        List<String> category = Arrays.asList(categoryList.split(","));
        List<ErpCollectProductCategory> categoryList1 = iErpCollectProductCategoryService.getCategoryList(category);
        return R.ok(categoryList1);
    }

    /**
     * 获取一级商品类目
     */
    @GetMapping("/selectTopList")
    public R<List<ErpCollectProductCategory>> selectTopList() {
        List<ErpCollectProductCategory> list = iErpCollectProductCategoryService.selectTopList();
        return R.ok(list);
    }

    /**
     * 查询商品类目列表
     */
    @GetMapping("/selectList")
    public R<Object> selectList(ErpCollectProductCategoryBo bo) {
        List<Object> list = iErpCollectProductCategoryService.selectList(bo);
        return R.ok(list);
    }

    /**
     * 查询采集商品类目列表
     */
//    @SaCheckPerm/**/ission("gather:collectProductCategory:list")
    @GetMapping("/list")
    public R<List<ErpCollectProductCategoryVo>> list(ErpCollectProductCategoryBo bo) {
        List<ErpCollectProductCategoryVo> list = iErpCollectProductCategoryService.queryList(bo);
        return R.ok(list);
    }


    /**
     * 获取采集商品类目详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("gather:collectProductCategory:query")
    @GetMapping("/{id}")
    public R<ErpCollectProductCategoryVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(iErpCollectProductCategoryService.queryById(id));
    }

    /**
     * 新增采集商品类目
     */
//    @SaCheckPermission("gather:collectProductCategory:add")
    @Log(title = "采集商品类目", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpCollectProductCategoryBo bo) {
        return toAjax(iErpCollectProductCategoryService.insertByBo(bo));
    }

    /**
     * 修改采集商品类目
     */
//    @SaCheckPermission("gather:collectProductCategory:edit")
    @Log(title = "采集商品类目", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpCollectProductCategoryBo bo) {
        return toAjax(iErpCollectProductCategoryService.updateByBo(bo));
    }

    /**
     * 删除采集商品类目
     *
     * @param ids 主键串
     */
//    @SaCheckPermission("gather:collectProductCategory:remove")
    @Log(title = "采集商品类目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(iErpCollectProductCategoryService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    @GetMapping("/selectAlreadyList")
    public R<Object> selectAlreadyList() {
        List<Object> list = iErpCollectProductCategoryService.selectAlreadyList();
        return R.ok(list);
    }

    @GetMapping("/selectStatisticsList")
    public R<Object> selectStatisticsList(String categoryName, Long brandId) {
        List<ErpCollectProductCategoryDto> list = iErpCollectProductCategoryService.selectStatisticsList(categoryName,
                brandId);
        return R.ok(list);
    }

    /**
     * 查询下级类目
     */
    @GetMapping("/querySubordinateCategory")
    public R<Object> querySubordinateCategory(String categoryId) {
        List<ErpCollectProductCategory> list = iErpCollectProductCategoryService.querySubordinateCategory(categoryId);
        return R.ok(list);
    }

    @GetMapping("/createPrimaryCategory")
    public R<Object> createPrimaryCategory() {
        return iErpCollectProductCategoryService.createPrimaryCategory();
    }

    /**
     * 获取有权限的测试发布店铺
     * @return
     */
    @GetMapping("/getCategoryTreeListForStore/{categoryId}")
    public List<Long> getCategoryTreeListForStore(@PathVariable("categoryId") Long categoryId) {
        return iErpCollectProductCategoryService.getCategoryTreeListForStore(categoryId);
    }

    /**
     * @Description: 根据类目名称，模糊查询类目ID集合
     * @Author: j.zh
     * @Date: 2024/6/9
     * @param categoryName
     * @return
     * @Version: 4.0.0
    **/
    @GetMapping("/getCategoryIdListByCategoryName/{categoryName}")
    public R<Object> getCategoryIdListByCategoryName(@PathVariable("categoryName") String categoryName) {
        // 异常处理
        List<Long> categoryIdList = iErpCollectProductCategoryService.getCategoryIdListByCategoryName(categoryName.replaceAll("@@@", "/"));
        if (categoryIdList != null && categoryIdList.size() > 0){
            return R.ok(categoryIdList);
        }
        return R.fail();
    }
}
